package test;

import javax.sound.sampled.Port;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author tongchen
 * @create 2023-02-20 15:01
 */
public class TcpMsgServer {
    //创建socket
private ServerSocket socket;
    public TcpMsgServer(int port) throws IOException {
        this.socket=new ServerSocket(port);
    }
    //开启工作
    public void start() throws IOException {
        System.out.println("用户端开启成功......");
        //循环接收用户请求
        //创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10));
        while(true){
            //接收用户请求
            Socket clientSocket = socket.accept();
            //处理用户请求（使用多线程）
            threadPoolExecutor.submit(()->{
                try {
                    pocessRequset(clientSocket) ;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }

    private void pocessRequset(Socket clientSocket) throws IOException {
        System.out.printf("%s|%d,客服端连接成功......\n",clientSocket.getInetAddress().toString(),clientSocket.getPort());
        //获取两个流
        try(InputStream inputStream=clientSocket.getInputStream(); OutputStream outputStream=clientSocket.getOutputStream()){
            //多次获取用户请求
            while(true){
                //获取用户输入
                Scanner scan=new Scanner(inputStream);
                if(!scan.hasNext()){
                    System.out.printf("%s|%d,客服端退出成功......",clientSocket.getInetAddress().toString(),clientSocket.getPort());
                    break;
                }
                String request= scan.nextLine();
                //根据请求获取响应
                String response=pocess(request);
                //通过print发回
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.println(response);
                //打印效果
                printWriter.flush();
                System.out.printf("[%s:%d] request = %s, response = %s\n", clientSocket.getInetAddress().toString(),
                        clientSocket.getPort(), request, response);

            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            clientSocket.close();
        }
    }

    private String pocess(String request) {
        System.out.println("请求为"+request);
        System.out.println("请输入你的响应->");
        Scanner scanner=new Scanner(System.in);
        String response=scanner.nextLine();
        return response;
    }
    //开启
    public static void main(String[] args) throws IOException {
        TcpMsgServer msgServer = new TcpMsgServer(9090);
        msgServer.start();
    }
}
